<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="../vue.js"></script>
    <style>
      * {
        margin: 0;
        padding: 0;
        list-style: none;
      }
      .mark {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        background: rgba(0, 0, 0, 0.4);
      }

      .text {
        width: 400px;
        height: 400px;
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        margin: auto;
        background: #fff;
      }
      #app {
        width: 900px;
      }
      li {
        overflow: hidden;
        width: 810px;
        list-style: none;
        margin-left: 40px;
      }
      input {
        width: 800px;
        margin-left: 40px;
        height: 30px;
        line-height: 30px;
        margin-bottom: 20px;
      }
      li span {
        float: left;
        width: 200px;
        height: 40px;
        border: 1px solid rgb(138, 144, 144);
        text-align: center;
        line-height: 40px;
      }
      li:nth-of-type(1) button {
        display: none;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <input
        type="text"
        name=""
        id=""
        v-model="title"
        @keyup.enter="sendHandle()"
        placeholder="请输入图书名称"
        @keyup.enter="sendHandle()"
      />
      <ul>
        <li v-for="item in arr" :key="item.id">
          <span>{{item.id}}</span>
          <span>{{item.title}}</span>
          <span>{{item.time}}</span>
          <span
            >{{item.active}}
            <button @click="isShow(item.id)">详情</button>
            <button @click="delHandle(item.id)">删除</button>
          </span>
        </li>
      </ul>
      <my-dialog v-show="flag" @send="changeFlag"></my-dialog>
    </div>
    <!-- 弹框 -->
    <template id="dialog">
      <div class="mark">
        <div class="text">
          <ul>
            <li v-for="(item, index) in list">
              {{ item.title }}
              <p>{{item.time}}</p>
            </li>
          </ul>

          <button @click="cancel">退出详情</button>
        </div>
      </div>
    </template>
    <script>
      const Dialog = {
        template: "#dialog",
        data() {
          return {
            list: [
              {
                title: "三国演义",
                time: "2022/7/13",
              },
              {
                title: "水浒传",
                time: "2022/7/13",
              },
              {
                title: "三国演义",
                time: "2022/7/13",
              },
            ],
          };
        },
        methods: {
          //取消
          cancel() {
            this.$emit("send", {
              flag: false,
            });
          },
        },
      };
      var vm = new Vue({
        el: "#app",
        data: function () {
          return {
            // 页面所有显示内容---状态 都需要在数据中定义好
            arr: [
              { id: "id", title: "书名", time: "添加时间", active: "操作" },
              { id: 1, title: "三国演义", time: "2022/7/13" },
              { id: 2, title: "水浒传", time: "2022/7/13" },
            ],
            title: "",
            time: "2022/7/13",
            flag: false,
          };
        },
        components: {
          "my-dialog": Dialog,
        },
        methods: {
          // 发布
          sendHandle() {
            console.log("发布");
            //
            var obj = {
              id: Date.now(),
              title: this.title,
              time: this.time,
            };
            this.arr.push(obj);

            // 清空
            this.title = "";
          },
          isShow(id) {
            console.log(id);
            this.flag = true;
            /*   this.arr = this.arr.filter((v) => v.id == id); */
          },
          // 取消
          changeFlag({ flag, list }) {
            this.flag = flag;
          },
          //删除
          delHandle(id) {
            this.arr = this.arr.filter((v) => v.id != id);
          },
        },
      });
    </script>
  </body>
</html>
